import request from '@/config/axios'
import type { DataData } from './types'

export const getDataListApi = (params: any): Promise<IResponse> => {
    const queryParams = params.queryParams ? params.queryParams : {}
    if (params.queryParams) {
        delete params.queryParams
    }
    Object.assign(params, queryParams)
    return request.get({ url: '/data/', params })
}
export const saveOrUpdate = (data: Partial<DataData>, isUpdate = false): Promise<IResponse> => {
    if (isUpdate) {
        return request.put({ url: '/data/', data })
    } else {
        return request.post({ url: '/data/', data })
    }
}

export const getDataDetailApi = (id: string | number): Promise<IResponse<DataData>> => {
    return request.get({url: `/data/${id}`, params: {id}})
}

export const delDataApi = (data: Partial<DataData>[]): Promise<IResponse> => {
    if (data.length > 0) {
        const ids = data.map((d) => d.service_id);
        return request.delete({ url: `/data/delete/${ids.join(',')}` })
    } else {
        const id = data[0].service_id
        return request.delete({ url: `/data/${id}` })
    }
}

export const startDataApi = (id: string | number): Promise<IResponse> => {
    return request.post({url: `/data/start`, data: {id}})
}
export const stopDataApi = (id: string | number): Promise<IResponse> => {
    return request.post({url: `/data/stop`, data: {id}})
}

export const dataAuthApi = (data: Partial<DataData>): Promise<IResponse> => {
    return request.post({url: `/data/auth`, data})
}
export const dataLicenseApi = (data: Partial<DataData>): Promise<IResponse> => {
    return request.put({url: `/data/license`, data})
}
